﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.Modal
{
    public partial class BankBillEdit : BasePage
    {
        private YX_BankBillBLL bbbll = new YX_BankBillBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDateStart.Value = DateTime.Now.ToString("yyyy-MM-dd");

                BindBank();
                BindPayeeBank();
                BindCompany();
                BindPayeeCompany();

                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    BindData();
                }
            }
        }

        /// <summary>
        /// 修改绑定数据
        /// </summary>
        public void BindData()
        {
            string id = Request.QueryString["ID"].ToString();

            YX_BankBill model = bbbll.SelectById(id);

            if (model != null)
            {
                FORM_BANK_ID.SelectedValue = model.N_BANKBILL_DRAWERBANKID.ToString();
                FORM_COMPANY_ID.SelectedValue = model.N_BANKBILL_DRAWERCOMPANYID.ToString();
                FORM_PayeeBank.SelectedValue = model.N_BANKBILL_PAYEEBANKID.ToString();
                FORM_PayeeCompany.SelectedValue = model.N_BANKBILL_PAYEECOMPANYID.ToString();
                txtZnum.Value = model.C_BANKBILL_DRAWERACCOUNT;
                txtDfzh.Value = model.C_BANKBILL_PAYEEACCOUNT;
                txtamont1.Value = model.N_BANKBILL_INVOICEVALUE.ToString();
                txtDateStart.Value = model.D_BANKBILL_STARTDATE.ToString("yyyy-MM-dd");
                txtDateEnd.Value = model.D_BANKBILL_ENDDATE.ToString("yyyy-MM-dd");
                txtBanknum.Text = model.C_BANKBILL_CODE;
                //ddlCheckType.SelectedValue = model.N_BANKBILL_IFOFFER.ToString();
                txtRemark.Text = model.C_BANKBILL_REMARK;
            }
        }

        #region 初次加载公司和银行

        /// <summary>
        /// 得到汇票银行
        /// </summary>
        public void BindBank()
        {
            int count;

            var dt = new Sys_BankBLL().GetEntitys(null, null, 0, 1000, out count);

            FORM_BANK_ID.DataSource = dt;
            FORM_BANK_ID.DataTextField = "C_NAME";
            FORM_BANK_ID.DataValueField = "N_ID";
            FORM_BANK_ID.DataBind();
            FORM_BANK_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));

            FORM_BANK_ID.SelectedValue = "-1";
        }

        /// <summary>
        /// 收款客户银行
        /// </summary>
        public void BindPayeeBank()
        {
            int count;

            var dt = new Sys_BankBLL().GetEntitys2(null, null, 0, 1000, out count);

            FORM_PayeeBank.DataSource = dt;
            FORM_PayeeBank.DataTextField = "C_NAME";
            FORM_PayeeBank.DataValueField = "N_ID";
            FORM_PayeeBank.DataBind();
            FORM_PayeeBank.Items.Insert(0, new ListItem("---请选择---", "-1"));

            FORM_PayeeBank.SelectedValue = "-1";
        }

        /// <summary>
        /// 得到汇票公司
        /// </summary>
        public void BindCompany()
        {
            int count;

            var dt = new Sys_CompanyBLL().GetEntitys(null, null, 0, 1000, out count);

            FORM_COMPANY_ID.DataSource = dt;
            FORM_COMPANY_ID.DataTextField = "CMY_NAME";
            FORM_COMPANY_ID.DataValueField = "CMY_ID";
            FORM_COMPANY_ID.DataBind();
            FORM_COMPANY_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));

            FORM_COMPANY_ID.SelectedValue = "-1";
        }

        /// <summary>
        /// 收款客户公司
        /// </summary>
        public void BindPayeeCompany()
        {
            int count;

            var dt = new Yx_CustomerBLL().GetEntitys(null, null, 0, 1000, out count);

            FORM_PayeeCompany.DataSource = dt;
            FORM_PayeeCompany.DataTextField = "C_NAME";
            FORM_PayeeCompany.DataValueField = "N_ID";
            FORM_PayeeCompany.DataBind();
            FORM_PayeeCompany.Items.Insert(0, new ListItem("---请选择---", "-1"));

            FORM_PayeeCompany.SelectedValue = "-1";
        }

        #endregion 初次加载公司和银行

        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 编辑条件

            if (FORM_BANK_ID.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择出票人银行名称");
                return;
            }
            if (txtZnum.Value.Trim() == "")
            {
                MessageBox.Show(Page, "出票人账号不能为空");
                return;
            }

            if (txtDfzh.Value.Trim() == "")
            {
                MessageBox.Show(Page, "收款人账号不能为空");
                return;
            }

            if (txtamont1.Value.Trim() == "")
            {
                MessageBox.Show(Page, "出票金额不能为空");
                return;
            }
            else
            {
                decimal je = 0;
                if (!decimal.TryParse(txtamont1.Value.Trim(), out je))
                {
                    MessageBox.Show(Page, "出票金额必须是数字");
                    return;
                }
                else
                {
                    if (Convert.ToDecimal(txtamont1.Value.Trim()) > 10000000)
                    {
                        MessageBox.Show(Page, "票面金额最高为1000万元");
                        return;
                    }
                }
            }

            if (txtDateStart.Value.Trim() == "")
            {
                MessageBox.Show(Page, "出票日期不能为空");
                return;
            }

            if (txtDateEnd.Value.Trim() == "")
            {
                MessageBox.Show(Page, "最后期限不能为空");
                return;
            }

            bool mainName = SelectMethod();
            if (mainName == false)
            {
                MessageBox.Show(Page, "主账户要与银行账户设置相匹配!");
                return;
            }

            bool PayeeName = SelectMethod2();
            if (PayeeName == false)
            {
                MessageBox.Show(Page, "收款人账户要与客户银行账户设置相匹配!");
                return;
            }

            #endregion 编辑条件

            YX_BankBill model = new YX_BankBill();

            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                //修改
                string id = Request.QueryString["ID"].ToString();

                model = bbbll.SelectById(id);
            }
            else
            {
                model.N_BANKBILL_ID = 1;//N_DETAIL_LISTID
            }

            model.D_BANKBILL_STARTDATE = DateTime.Parse(txtDateStart.Value);
            model.D_BANKBILL_ENDDATE = DateTime.Parse(txtDateEnd.Value);
            model.N_BANKBILL_DRAWERBANKID = decimal.Parse(FORM_BANK_ID.SelectedValue);
            model.N_BANKBILL_DRAWERCOMPANYID = decimal.Parse(FORM_COMPANY_ID.SelectedValue);
            model.N_BANKBILL_PAYEEBANKID = decimal.Parse(FORM_PayeeBank.SelectedValue);
            model.N_BANKBILL_PAYEECOMPANYID = decimal.Parse(FORM_PayeeCompany.SelectedValue);
            model.C_BANKBILL_DRAWERACCOUNT = txtZnum.Value.Trim();
            model.C_BANKBILL_PAYEEACCOUNT = txtDfzh.Value.Trim();
            model.N_BANKBILL_INVOICEVALUE = decimal.Parse(txtamont1.Value);
            model.C_BANKBILL_CODE = txtBanknum.Text.Trim();
            model.C_BANKBILL_REMARK = txtRemark.Text.Trim();
            model.N_BANKBILL_IFOFFER = decimal.Parse(ddlCheckType.SelectedValue);

            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                //修改
                int count = bbbll.UPDATE(model, lognUser.USER_NAME);

                if (count > 0)
                {
                    MessageBox.Show(Page, "修改成功！");
                }
                else
                {
                    MessageBox.Show(Page, "修改失败！");
                }
            }
            else
            {
                //新增
                int count = bbbll.INSERT(model, lognUser.USER_NAME);

                if (count > 0)
                {
                    MessageBox.Show(Page, "新增成功！");
                }
                else
                {
                    MessageBox.Show(Page, "新增失败！");
                }
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_CLOSE();");
        }

        /// <summary>
        /// 根据输入的主账号查询银行账户设置的主账号
        /// </summary>
        private bool SelectMethod()
        {
            BaseBLL bsBLL = new BaseBLL();
            DataTable dt = null;
            string mainName = this.txtZnum.Value.ToString();

            string sql = " and a.accountno = '" + mainName + "'";

            {
                string _sql = @"select a.accounti_id accountId,
       c.cmy_name companyName,
       d.deptname depName,
       decode(a.accounttype, 1, '支出户', 2, '收入户') accType,
       a.accountno accountNo,
       b.c_name openBank
  from sys_company c, sys_department d, yx_account a,sys_bank b
 where c.cmy_id = d.cmy_id
   and c.cmy_id = a.company_id
   and d.deptid = a.department_id
   and a.n_bank_id=b.n_id";
                if (_sql != "")
                {
                    _sql += sql;
                }
                dt = bsBLL.Query(_sql);
            }

            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据输入的客户账号查询银行账户设置的账号
        /// </summary>
        private bool SelectMethod2()
        {
            BaseBLL bsBLL = new BaseBLL();
            DataTable dt = null;
            string mainName = this.txtDfzh.Value.ToString();

            string sql = " where c_number= '" + mainName + "'";

            {
                string _sql = @"select n_id, n_customer_id, c_realname, c_number, n_bank_id, d_operator_date, n_operator_id from yx_customer_bank";
                if (_sql != "")
                {
                    _sql += sql;
                }
                dt = bsBLL.Query(_sql);
            }

            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}